FOPEN

Section: MINTLIB LIBRARY FUNCTIONS (3)
Updated: 3 March 1993
Index Return to Main Contents
 

NAME

fopen, freopen, fdopen - open a stream  

SYNOPSIS

#include <stdio.h>

FILE *fopen(const char *filename, const char *type);

FILE *freopen(const char *filename, const char *type,
              FILE *stream);

FILE *fdopen(int fd, const char *type);
 

DESCRIPTION

fopen opens the file named by filename and associates a stream with it. If the open succeeds, fopen returns a pointer to be used to identify the stream in subsequent operations. filename points to a character string that contains the name of the file to be opened. type is a character string starting with one the following values:

  r   - open for reading
  w   - truncate or create for writing
  a   - append: open for writing at end of file, or create
        for writing
  r+  - open for update (reading and writing)
  w+  - truncate or create for update
  a+  - append; open or create for update at EOF After one of the above, type may be followed by:

  t   - open file in text mode (translate rn to n when
        reading, and n to rn when writing)
  b   - open file in binary mode (no r, n translation) If neither 't' nor 'b' is set, the file will be opened in the default mode set by the user. If the environment variable UNIXMODE contains the letter 'b', the default mode will be binary; otherwise, the default mode is text mode. freopen opens the file named by filename and associates the stream pointed to by stream with it. The type argument is used just as in fopen. The original stream is closed, regardless of whether the open ultimately succeeds. If the open succeeds, freopen returns the original value of stream. freopen is typically used to attach the preopened streams associated with stdin, stdout, and stderr to other files. fdopen associates a stream with the file descriptor fd. File descriptors are obtained from calls like open, dup, creat, or pipe, which open files but do not return streams. Streams are necessary input for many of the library routines. The type of the stream must agree with the access permissions of the open file. When a file is opened for update, both input and output may be done on the resulting stream. However, output may not be directly followed by input without an intervening fseek or rewind, and input may not be directly followed by output without an intervening fseek, rewind, or an input operation which encounters EOF.  

RETURN VALUES

On success, fopen, freopen, and fdopen return a pointer to FILE which identifies the opened stream. On failure, they return NULL.  

SEE ALSO

open(3), pipe(3), fclose(3), fseek(3)  

BUGS

The library allows only a limited number of streams to be open at the same time. Thus, a program may run out of streams even though the system is not yet out of memory.
 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUES
SEE ALSO
BUGS

This document was created by man2html, using the manual pages.
Time: 11:15:00 GMT, June 22, 2025